package cn.ohyeah.itvgame.plumber;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


/**
 * Game Choose Interface
 * 
 * @author peyton.zhong
 * @since 2011-7-5
 */
public class GameChoose extends Canvas {
  Image backGround_menu;
  Image mainMenuImg;
  int menuWidth, menuHeight;

  Image[][] menu_button;
  Image helpImg;
  Image rankingImg;
  public static Graphics g = null;
  int cursorInd = 0; // menu position 0: new game; 1: help; 2: ranking; 3:quit
  boolean isChosen = false;

  public GameChoose() {
    try {
      backGround_menu = Image.createImage("/res/backGround_menu.jpg");
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      mainMenuImg = Image.createImage("/res/menu_button.png");
      menuWidth = mainMenuImg.getWidth() / 2;
      menuHeight = mainMenuImg.getHeight() / 4;
      System.out
          .println("menuWidth=" + menuWidth + ";menuHeight=" + menuHeight);
      menu_button = new Image[4][2];

      for (int i = 0; i < menu_button.length; i++) {// Y
        for (int j = 0; j < menu_button[i].length; j++) {// X
          menu_button[i][j] = Image.createImage(menuWidth, menuHeight);
          g = menu_button[i][j].getGraphics();
          System.out.println("i=" + i + ";j=" + j);
          g.drawImage(mainMenuImg, -j * menuWidth, -i * menuHeight,
              Tools.GRAPHCS_TOP_LEFT);
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      helpImg = Image.createImage("/res/help.jpg");
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      rankingImg = Image.createImage("/res/ranking.jpg");
    } catch (Exception e) {
      e.printStackTrace();
    }
    cursorInd = 0;
    isChosen = false;
  }

  protected synchronized void keyPressed(int keyCode) {
    if (isChosen) {
      isChosen = false;
      repaint();
      return;
    }
    int key_GameCode = getGameAction(keyCode);
    switch (key_GameCode) {
    case Canvas.UP:
      System.out.println("up");
      cursorInd--;
      if (cursorInd < 0) {
        cursorInd = menu_button.length - 1;
      }
      break;
    case Canvas.DOWN:
      System.out.println("down");
      cursorInd++;
      if (cursorInd > menu_button.length - 1) {
        cursorInd = 0;
      }
      break;
    case Canvas.FIRE:
      isChosen = true;
      if (cursorInd == 0) {
        PlumberMidlet.mainMidlet.enterPlayStyleOne();
      } else if (cursorInd == 1) {// help
      } else if (cursorInd == 2) {// ranking
      } else if (cursorInd == 3) {// quit
      }
      System.out.println("fire");
      break;
    }
    repaint();
  }

  protected void paint(Graphics g) {
    if (isChosen) {
      if (cursorInd == 1) {// help
        g.drawImage(helpImg, 0, 0, Tools.GRAPHCS_TOP_LEFT);
      } else if (cursorInd == 2) {// ranking
        g.drawImage(rankingImg, 0, 0, Tools.GRAPHCS_TOP_LEFT);
      }
    } else {
      g.drawImage(backGround_menu, 0, 0, Tools.GRAPHCS_TOP_LEFT);
      int menuOffsetX = 250;
      int menuOffsetY = 289;

      for (int i = 0; i < 4; i++) {
        System.out.println("x=" + menuOffsetX + ";y="
            + (menuOffsetY + menuHeight * i));
        g.setClip(menuOffsetX, menuOffsetY + menuHeight * i, menuWidth,
            menuHeight);
        int menuX, menuY;
        if (i == cursorInd) {
          // g.drawImage(mainMenuImg, menuOffsetX - menuWidth, menuOffsetY,
          // Tools.GRAPHCS_TOP_LEFT);
          menuX = menuOffsetX - menuWidth;
          menuY = menuOffsetY;
        } else {
          // g.drawImage(mainMenuImg, menuOffsetX, menuOffsetY,
          // Tools.GRAPHCS_TOP_LEFT);
          menuX = menuOffsetX;
          menuY = menuOffsetY;
        }
        System.out.println("menuX=" + menuX + ";menuY=" + menuY);
        g.drawImage(mainMenuImg, menuX, menuY, Tools.GRAPHCS_TOP_LEFT);
      }
    }
  }
}
